package com.gyms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.okhttp.beans.HVProductInfoBean;
import com.gyms.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import glide.ImageLoaderUtils;
import utils.DecimalUtils;
import utils.StringUtils;

/**
 * 所有课程的适配器 yjl
 */
public class AllCourseAdapter extends BaseAdapter {
    private List<HVProductInfoBean> objects;

    private Context mContext;
    private LayoutInflater layoutInflater;

    public AllCourseAdapter(Context context, List<HVProductInfoBean> list) {
        this.mContext = context;
        objects = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<HVProductInfoBean> objects) {
        this.objects.addAll(objects);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public HVProductInfoBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_all_course, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            //对于listview，注意添加这一行，即可在item上使用高度
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        initializeViews(getItem(position), holder);
        return convertView;
    }

    private void initializeViews(HVProductInfoBean object, ViewHolder mViewHolder) {
        if(!StringUtils.isNull(object)) {
            if(!StringUtils.isNull(object.getDiscount())){
                mViewHolder.mLlDiscound.setVisibility(View.GONE);
                String discountUrl=object.getDiscount().get(0).getDiscountIcon();
                String mDiscountContent=object.getDiscount().get(0).getDiscountContent();//优惠文本
                if(!StringUtils.isNull(discountUrl)){//优惠的图片为null隐藏
                    mViewHolder.mLlDiscound.setVisibility(View.VISIBLE);
                    mViewHolder.mTvDiscount.setVisibility(View.VISIBLE);
                    ImageLoaderUtils.displayBigPhoto(mContext, mViewHolder.mTvDiscount, discountUrl);

                }else{
                    mViewHolder.mTvDiscount.setVisibility(View.GONE);
                }
                mViewHolder.tvProductHui.setText(StringUtils.CS(mDiscountContent));
            }else{
                mViewHolder.mTvDiscount.setVisibility(View.GONE);
                mViewHolder.mLlDiscound.setVisibility(View.GONE);
            }

            mViewHolder.tvProductTitle.setText(StringUtils.CS(object.getProductName()));
            mViewHolder.tvAddressDetil.setText(StringUtils.CS(object.getShortAddress()));
            if(!StringUtils.isNull(object.getMinPrice())&&!StringUtils.isNull(object.getMaxPrice())) {
                if (object.getMinPrice().compareTo(object.getMaxPrice()) == 0) {
//                    mViewHolder.tvPrise.setText("￥" + DecimalUtils.doubleToIntOrD(object.getMinPrice()));
                    mViewHolder.tvPrise.setText( DecimalUtils.setPriceStyle(mContext,object.getMinPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
                } else {
//                    mViewHolder.tvPrise.setText("￥" + DecimalUtils.doubleToIntOrD(object.getMinPrice()) + "-" + DecimalUtils.doubleToIntOrD(object.getMaxPrice()));
                    mViewHolder.tvPrise.setText(DecimalUtils.setPriceStyle(mContext,object.getMinPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true).append(" - ").append( DecimalUtils.setPriceStyle(mContext,object.getMaxPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,false)));
                }
            }
            if(!StringUtils.isNull(object.getDistance())) {
                double dis = object.getDistance() / 1000;
                showDis(mViewHolder,dis);
            }

            boolean isCollection=object.getIsCollection();//收藏
            boolean isRecommend=object.getIsRecommend();//推荐
            if(!isCollection&&!isRecommend){
                mViewHolder.ivRecommend.setVisibility(View.GONE);
            }else{
                mViewHolder.ivRecommend.setVisibility(View.VISIBLE);
            }
            if (isRecommend) {
                mViewHolder.ivRecommend.setImageResource(R.mipmap.icon_best);
            }
            if (isCollection) {//收藏大于推荐
                mViewHolder.ivRecommend.setImageResource(R.mipmap.icon_save_subscript);
            }

            ImageLoaderUtils.display(mContext, mViewHolder.ivProductCover, object.getProductUrl());
        }
    }

    /**
     * 小于1公里的，距离显示“<1km”；大于等于1公里，小于等于50公里的，距离显示“xx.xkm”；大于50公里的，距离显示“>50km”；
     *
     * @param dis 公里
     */
    private void showDis(ViewHolder mViewHolder,double dis) {
        String disText;
        if (dis < 1) {
            mViewHolder.tvDistance.setText("<1km");
        } else if (dis >= 1 && dis <= 50) {
            disText=DecimalUtils.getDistaceText(dis);
            mViewHolder.tvDistance.setText(disText + "km");
        } else {
            mViewHolder.tvDistance.setText(">50km");
        }
    }
    static class ViewHolder {
        @BindView(R.id.iv_product_cover)
        ImageView ivProductCover;
        @BindView(R.id.tv_product_title)
        TextView tvProductTitle;
        @BindView(R.id.tv_address_detil)
        TextView tvAddressDetil;
        @BindView(R.id.tv_distance)
        TextView tvDistance;
        @BindView(R.id.tv_product_hui)
        TextView tvProductHui;
        @BindView(R.id.tv_prise)
        TextView tvPrise;
        @BindView(R.id.iv_recommend)
        ImageView ivRecommend;
        @BindView(R.id.tv_discount)
        ImageView mTvDiscount;//优惠
         @BindView(R.id.ll_discound)
         LinearLayout mLlDiscound;//优惠
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
